import json
from es_option.es_api import (
    search_similar_keyword_vector,
    search_similar_field_vector,
    search_similar_reading_type_vector,
)
from elasticsearch import Elasticsearch
import numpy as np
import os
import sys

# 实现智能推荐
# 选择题

es = Elasticsearch(["http://localhost:9200"])
id = "e5134229-642b-4ad0-95fc-72d01ecf4826"
translation_id = "3277dc94-f9a5-47a0-9729-c606e8fd05b8"
writing_id = "7983e045-4d66-482d-af41-21f46ef443a5"
reading_id = "e0271cac-791f-4bcd-8107-780c5775fa98"
"""
wrong_question
original_question
"""


# 计算两个list数组的文本相似度
def cos_sim(nums1, nums2):
    nums1 = np.array(nums1)
    nums2 = np.array(nums2)
    temp_res = np.dot(nums1, nums2)
    nums1 = np.linalg.norm(nums1)
    nums2 = np.linalg.norm(nums2)
    return temp_res / (nums1 * nums2)


# 选择题的智能推荐，根据ID查询出对应的数据
def intelligent_recommendation_choice(id):
    """
    找到知识点匹配度最高的，然后判断，文本相似度是不是小于80%
    :param id:
    :return:
    """
    question_type = "choice"
    wrong_question = es.get(index="wrong_question", id=id)
    wrong_question = wrong_question  # 此时就是dict
    wrong_question = wrong_question["_source"]
    wrong_question_original_vector = wrong_question["original_vector"]
    keyword_vector = wrong_question["keyword_vector"]  # 选择题的关键词，就是知识点
    search_keyword = search_similar_keyword_vector(
        "original_question", keyword_vector, question_type
    )
    # print(search_keyword)
    search_id = []
    mismatching_id = []
    # 排序完成的选择题
    # 确保文本匹配度小于0.9
    for item in search_keyword:
        # 文本相似度
        cos_res = cos_sim(wrong_question_original_vector, item["original_vector"])
        # print(cos_res)
        if cos_res <= 0.90:
            # print(item['id'])
            search_id.append(item["id"])
            # print(f"search_id为：{search_id}")
            if len(search_id) >= 3:
                break
        else:
            mismatching_id.append(item["id"])
    if len(search_id) == 0:
        search_id.append(mismatching_id[0])
        search_id.append(mismatching_id[1])
        search_id.append(mismatching_id[2])
    if len(search_id) == 1:
        search_id.append(mismatching_id[0])
        search_id.append(mismatching_id[1])
    if len(search_id) == 2:
        search_id.append(mismatching_id[0])
    # print(search_id)
    return search_id


# 翻译题的智能推荐
# 原文小于0.9
# 领域一致
def intelligent_recommendation_translation(id):
    question_type = "translation"
    wrong_question = es.get(index="wrong_question", id=id)
    wrong_question = wrong_question  # 此时就是dict
    wrong_question = wrong_question["_source"]
    wrong_question_original_vector = wrong_question["original_vector"]
    filed_vector = wrong_question["field_vector"]
    search_field = search_similar_field_vector(
        "original_question", filed_vector, question_type
    )
    search_id = []
    mismatching_id = []
    for item in search_field:
        # 文本相似度
        cos_res = cos_sim(wrong_question_original_vector, item["original_vector"])
        # print(cos_res)
        if cos_res <= 0.90:
            # print(item['id'])
            search_id.append(item["id"])
            # print(f"search_id为：{search_id}")
            if len(search_id) >= 3:
                break
        else:
            mismatching_id.append(item["id"])
    if len(search_id) == 0:
        search_id.append(mismatching_id[0])
        search_id.append(mismatching_id[1])
        search_id.append(mismatching_id[2])
    if len(search_id) == 1:
        search_id.append(mismatching_id[0])
        search_id.append(mismatching_id[1])
    if len(search_id) == 2:
        search_id.append(mismatching_id[0])
    # print(search_id)
    return search_id


# 作文的智能推荐
# 领域排序
# 体裁大于0.9
# 文本相似度小于0.9
def intelligent_recommendation_writing(id):
    question_type = "writing"
    wrong_question = es.get(index="wrong_question", id=id)
    wrong_question = wrong_question  # 此时就是dict
    wrong_question = wrong_question["_source"]
    wrong_question_original_vector = wrong_question["original_vector"]
    wrong_question_genre_vector = wrong_question["genre_vector"]
    filed_vector = wrong_question["field_vector"]
    search_field = search_similar_field_vector(
        "original_question", filed_vector, question_type
    )
    search_id = []
    mismatching_id = []
    for item in search_field:
        # 计算文本相似度
        cos_ori_res = cos_sim(wrong_question_original_vector, item["original_vector"])
        # print(cos_ori_res)
        # print(item['id'])
        if cos_ori_res <= 0.90:
            # 计算体裁相似度
            cos_gen_res = cos_sim(wrong_question_genre_vector, item["genre_vector"])
            # print(cos_gen_res)
            if cos_gen_res >= 0.9:
                search_id.append(item["id"])
                if len(search_id) >= 3:
                    break
            else:
                mismatching_id.append(item["id"])
        else:
            mismatching_id.append(item["id"])

    if len(search_id) == 0:
        search_id.append(mismatching_id[0])
        search_id.append(mismatching_id[1])
        search_id.append(mismatching_id[2])
    if len(search_id) == 1:
        search_id.append(mismatching_id[0])
        search_id.append(mismatching_id[1])
    if len(search_id) == 2:
        search_id.append(mismatching_id[0])
    # print(search_id)
    return search_id


# 基于阅读题的智能推荐 分为两种情况，题型，以及，领域
# 题型，按题型排序，文本相似度小于0.9，
# 领域，按领域排序，文本相似度小于0.9
def intelligent_recommendation_reading(id, recommendation_way):
    """

    :param id:
    :param recommendation_way:  reading_type field
    :return:
    """
    question_type = "reading"
    wrong_question = es.get(index="wrong_question", id=id)
    wrong_question = wrong_question  # 此时就是dict
    wrong_question = wrong_question["_source"]
    wrong_question_original_vector = wrong_question["original_vector"]
    filed_vector = wrong_question["field_vector"]
    reading_type_vector = wrong_question["reading_type_vector"]
    search_id = []
    mismatching_id = []
    if recommendation_way == "reading_type":
        # 题型的相似度最高
        search_reading_type = search_similar_reading_type_vector(
            "original_question", reading_type_vector, question_type
        )
        for item in search_reading_type:
            cos_res = cos_sim(wrong_question_original_vector, item["original_vector"])
            if cos_res < 0.9:
                search_id.append(item["id"])
                if len(search_id) >= 3:
                    break
            else:
                mismatching_id.append(item["id"])
        if len(search_id) == 0:
            search_id.append(mismatching_id[0])
            search_id.append(mismatching_id[1])
            search_id.append(mismatching_id[2])
        if len(search_id) == 1:
            search_id.append(mismatching_id[0])
            search_id.append(mismatching_id[1])
        if len(search_id) == 2:
            search_id.append(mismatching_id[0])

    if recommendation_way == "field":
        search_field = search_similar_field_vector(
            "original_question", filed_vector, question_type
        )
        for item in search_field:
            cos_res = cos_sim(wrong_question_original_vector, item["original_vector"])
            if cos_res < 0.9:
                search_id.append(item["id"])
                if len(search_id) >= 3:
                    break
            else:
                mismatching_id.append(item["id"])
        if len(search_id) == 0:
            search_id.append(mismatching_id[0])
            search_id.append(mismatching_id[1])
            search_id.append(mismatching_id[2])
        if len(search_id) == 1:
            search_id.append(mismatching_id[0])
            search_id.append(mismatching_id[1])
        if len(search_id) == 2:
            search_id.append(mismatching_id[0])

    # print(search_id)
    return search_id


# intelligent_recommendation_reading(reading_id,"field")
# intelligent_recommendation_writing(writing_id)
# intelligent_recommendation_translation(translation_id)
# intelligent_recommendation_choice(id)

if __name__ == "__main__":

    type = sys.argv[2]
    # 获取题目的类型
    type = sys.argv[1]
    # 获取题目的esID
    esid = sys.argv[2]
    # type = "fanyi"
    # esid = "4fa6740c-85c6-4550-a886-a177e9a0c573"
    # 如果题目类型是阅读，需要指定推荐方式
    if type == "yuedu":
        way = sys.argv[3]
        # way = "reading_type"
        search_id = intelligent_recommendation_reading(esid, way)
    elif type == "xuanze":
        search_id = intelligent_recommendation_choice(esid)
    elif type == "fanyi":
        search_id = intelligent_recommendation_translation(esid)
    else:
        search_id = intelligent_recommendation_writing(esid)
    # search_id = [4, 10, 5]
    for i in range(3):
        print(search_id[i])
